package com.cabal.killed.service.impl;

import com.cabal.auth.dao.CabalAuthMapper;
import com.cabal.auth.dto.SendKilledMessageDTO;
import com.cabal.auth.entity.CabalCharacter;
import com.cabal.auth.vo.CabalAuthVo;
import com.cabal.common.util.CabalGatewayMD5Util;
import com.cabal.config.DynamicDataSourceContextHolder;
import com.cabal.killed.service.KilledService;
import com.cabal.socket.KilledWebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class KilledServiceImpl implements KilledService {

    private static Map<Integer, String> KILLED_NOTICE_CONTENT_MAP = new HashMap<>();

    public static Map<Integer, String> KILLED_MAP_MAP = new HashMap<>();

    public static Map<Integer, String> KILLED_CHANNEL_MAP = new HashMap<>();

    public static Map<Integer, String> PROFESSION_MAP = new HashMap<>();

    @Value("${md5.secret_key}")
    private String secretKey;

    @Resource
    private CabalAuthMapper cabalAuthMapper;

    static {
        KILLED_NOTICE_CONTENT_MAP.put(1, "$14#[First blood] $34#[%s][%s]$37#在$31#『%s %s』$37#于千军万马之中怒斩一血！");
        KILLED_NOTICE_CONTENT_MAP.put(2, "$14#[Double kill] $34#[%s][%s]$37#在$31#『%s %s』$37#于千军万马之中怒斩两人 $37#二连击破！");
        KILLED_NOTICE_CONTENT_MAP.put(3, "$14#[Triple kill] $34#[%s][%s]$37#在$31#『%s %s』$37#于千军万马之中怒斩三人 $37#三连决胜！！");
        KILLED_NOTICE_CONTENT_MAP.put(4, "$14#[Quadra kill] $34#[%s][%s]$37#在$31#『%s %s』$37#于千军万马之中怒斩四人 $37#四连超凡！！");
        KILLED_NOTICE_CONTENT_MAP.put(5, "$14#[Legendary kill] $34#[%s][%s]$37#在$31#『%s %s』$37#于千军万马之中怒斩五人 $37#五连绝世！");

        KILLED_MAP_MAP.put(1, "血色冰峰");
        KILLED_MAP_MAP.put(2, "呼啸沙漠");
        KILLED_MAP_MAP.put(3, "暮色雨林");
        KILLED_MAP_MAP.put(4, "勒克斯港");
        KILLED_MAP_MAP.put(5, "雷纳堡");
        KILLED_MAP_MAP.put(6, "黄昏湖畔 ");
        KILLED_MAP_MAP.put(7, "安息之地");
        KILLED_MAP_MAP.put(8, "遗忘废墟");
        KILLED_MAP_MAP.put(9, "黄昏要塞");
        KILLED_MAP_MAP.put(10, "机械之都");
        KILLED_MAP_MAP.put(11, "魔界地狱");
        KILLED_MAP_MAP.put(12, "破碎荒野");
        KILLED_MAP_MAP.put(16, "国战地图");
        KILLED_MAP_MAP.put(22, "罪恶之地");
        KILLED_MAP_MAP.put(30, "异次元空间");
        KILLED_MAP_MAP.put(50, "[QL]火山心脏的邀请函(团队副本)");
        KILLED_MAP_MAP.put(126, "损坏的亡者护符");
        KILLED_MAP_MAP.put(115, "帕莱斯特拉炼狱");
        KILLED_MAP_MAP.put(24, "黑暗印章");
        KILLED_MAP_MAP.put(57, "疾驰列车");
        KILLED_MAP_MAP.put(88, "亡者的护身符 (B3F)");
        KILLED_MAP_MAP.put(121, "亡者的护身符B3F（Part2）");
        KILLED_MAP_MAP.put(127, "天灾遗迹B3F");
        KILLED_MAP_MAP.put(58, "新的冰冻线索");
        KILLED_MAP_MAP.put(90, "遗忘之岛（觉醒模式）");
        KILLED_MAP_MAP.put(107, "奇妙的试剂");
        KILLED_MAP_MAP.put(111, "幻影的启示录外传");
        KILLED_MAP_MAP.put(122, "玻璃瓶");
        KILLED_MAP_MAP.put(124, "恶魔的钥匙");
        KILLED_MAP_MAP.put(125, "恶魔的邀请函");
        KILLED_MAP_MAP.put(120, "神秘的石雕");
        KILLED_MAP_MAP.put(117, "古老的龙石板");
        KILLED_MAP_MAP.put(116, "照亮灵魂的灯火");
        KILLED_MAP_MAP.put(51, "古代先锋的邀请函(团队副本)");
        KILLED_MAP_MAP.put(85, "被污染的仙人掌");
        KILLED_MAP_MAP.put(87, "全息地图");
        KILLED_MAP_MAP.put(23, "天灾遗迹 B1");
        KILLED_MAP_MAP.put(86, "损坏的电路芯片");
        KILLED_MAP_MAP.put(96, "山地雷达");
        KILLED_MAP_MAP.put(81, "神秘的树枝");

        KILLED_CHANNEL_MAP.put(1, "一线");
        KILLED_CHANNEL_MAP.put(2, "二线");
        KILLED_CHANNEL_MAP.put(3, "三线");
        KILLED_CHANNEL_MAP.put(4, "四线");
        KILLED_CHANNEL_MAP.put(5, "五线");
        KILLED_CHANNEL_MAP.put(6, "六线");
        KILLED_CHANNEL_MAP.put(7, "七线");
        KILLED_CHANNEL_MAP.put(8, "八线");

        PROFESSION_MAP.put(1, "WA");
        PROFESSION_MAP.put(2, "BL");
        PROFESSION_MAP.put(3, "WI");
        PROFESSION_MAP.put(4, "FA");
        PROFESSION_MAP.put(5, "FS");
        PROFESSION_MAP.put(6, "FB");
    }

    @Override
    public String sendKilledMessage(SendKilledMessageDTO sendKilledMessageDTO) {
        log.info("接收来自客户端帐号{}的公告请求", sendKilledMessageDTO.getCharacterName());
        DynamicDataSourceContextHolder.removeContextKey();
        CabalAuthVo cabalAuthVo = cabalAuthMapper.getCabalAuthVoByAccount(sendKilledMessageDTO.getCharacterName());
        int userNum = cabalAuthVo.getUserNum();
        int startCharacterId = userNum * 8;
        int endCharacterId = startCharacterId + 5;

        //log.info("角色ID范围{}-{}", startCharacterId, endCharacterId);

        Integer killedNum = Integer.parseInt(sendKilledMessageDTO.getKilledNum());
        CabalCharacter cabalCharacter = cabalAuthMapper.getCabalCharacterByCharacterId(startCharacterId, endCharacterId);
        if(cabalCharacter == null) {
            log.info("没有找到在线角色");
            return "没有找到角色信息";
        }

        String charName = getCharsetName(cabalCharacter.getName());
        log.info("{}在线角色名称{}", sendKilledMessageDTO.getCharacterName(), charName);

        Integer channelIdx = cabalCharacter.getChannelIdx();
        Integer worldIdx = Integer.parseInt(sendKilledMessageDTO.getMapValue());

        try {
            // 拼装公告信息调用ServerSocket，发送公告信息
            String sendMessage = KILLED_NOTICE_CONTENT_MAP.get(killedNum);
            String mapMessage = StringUtils.isBlank(KILLED_MAP_MAP.get(worldIdx)) ?"异次元中心":KILLED_MAP_MAP.get(worldIdx);
            sendMessage = String.format(sendMessage, PROFESSION_MAP.get(cabalCharacter.getProfession()), charName, KILLED_CHANNEL_MAP.get(channelIdx), mapMessage);

            String base64Message = Base64.getEncoder().encodeToString(sendMessage.getBytes("GBK"));
            String encryptMessage = CabalGatewayMD5Util.md5(base64Message, secretKey, false);
            sendMessage = "GG|".concat(encryptMessage).concat("|").concat(base64Message);
            KilledWebSocketServer.sendMessageAll(sendMessage);
        } catch (Exception e) {
            log.error("消息发送失败>>>>", e);
        }
        return "SUCCESS";
    }

    private String getCharsetName(String dbName) {
        Charset latin1Charset = Charset.forName("ISO-8859-1");
        Charset gbkCharset = Charset.forName("GBK");

        byte[] latin1Bytes = dbName.getBytes(latin1Charset);
        String decodedText = new String(latin1Bytes, gbkCharset);
        return decodedText;
    }
}
